/**
 * Notes: 充值套餐包控制器 - 宠物门店预约小程序
 * Date: 2025-01-27
 */

const BaseController = require('./base_controller.js');
const RechargePackageService = require('../service/recharge_package_service.js');

class RechargePackageController extends BaseController {

	/**
	 * 获取充值套餐包列表
	 */
	async getPackageList() {
		let service = new RechargePackageService();
		return await service.getPackageList();
	}

	/**
	 * 获取套餐包详情
	 */
	async getPackageDetail() {
		// 数据校验
		let rules = {
			packageId: 'must|string|min:1|max:50|name=套餐包ID'
		};

		let input = this.validateData(rules);

		let service = new RechargePackageService();
		return await service.getPackageDetail(input.packageId);
	}

	/**
	 * 管理员获取充值套餐包列表（包含已下架）
	 */
	async getAdminPackageList() {
		await this.isAdmin();
		
		let service = new RechargePackageService();
		return await service.getAdminPackageList();
	}

	/**
	 * 添加充值套餐包
	 */
	async addPackage() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			name: 'required|string|min:2|max:20|name=套餐包名称',
			amount: 'required|int|min:1|max:1000000|name=充值金额',
			bonus: 'int|min:0|max:500000|default:0|name=赠送金额',
			isRecommended: 'bool|default:false|name=是否推荐',
			label: 'string|max:10|name=标签',
			description: 'string|max:200|name=描述'
		};

		let input = this.validateData(rules);

		let service = new RechargePackageService();
		return await service.addPackage(input);
	}

	/**
	 * 编辑充值套餐包
	 */
	async editPackage() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			packageId: 'required|string|min:1|max:50|name=套餐包ID',
			name: 'required|string|min:2|max:20|name=套餐包名称',
			amount: 'required|int|min:1|max:1000000|name=充值金额',
			bonus: 'int|min:0|max:500000|default:0|name=赠送金额',
			isRecommended: 'bool|default:false|name=是否推荐',
			label: 'string|max:10|name=标签',
			description: 'string|max:200|name=描述'
		};

		let input = this.validateData(rules);

		let service = new RechargePackageService();
		return await service.editPackage(input.packageId, input);
	}

	/**
	 * 切换套餐包状态（启用/禁用）
	 */
	async togglePackageStatus() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			packageId: 'required|string|min:1|max:50|name=套餐包ID',
			status: 'required|int|in:0,1|name=状态'
		};

		let input = this.validateData(rules);

		let service = new RechargePackageService();
		return await service.togglePackageStatus(input.packageId, input.status);
	}

	/**
	 * 删除充值套餐包
	 */
	async deletePackage() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			packageId: 'required|string|min:1|max:50|name=套餐包ID'
		};

		let input = this.validateData(rules);

		let service = new RechargePackageService();
		return await service.deletePackage(input.packageId);
	}
}

module.exports = RechargePackageController;